﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.ApplicationServices;

namespace Galaxy.AutoCAD.Databases
{
    public static class ObjectIdTool
    {
        #region 移动实体（定义为objectid的括展函数）
        public static void Move(this ObjectId id, Point3d sourcePt, Point3d targetPt)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Vector3d vector = targetPt - sourcePt;
                Matrix3d mt = Matrix3d.Displacement(vector);
                Entity ent = (Entity)tran.GetObject(id, OpenMode.ForWrite);
                ent.TransformBy(mt);
                tran.Commit();
            }
        }
        #endregion
        #region 选择实体（定义为objectid的括展函数）
        public static void Rotate(this ObjectId id, Point3d basePt, double angle)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Entity ent = (Entity)tran.GetObject(id, OpenMode.ForWrite);
                Matrix3d mt = Matrix3d.Rotation(angle, Vector3d.ZAxis, basePt);
                ent.TransformBy(mt);
                tran.Commit();
            }
        }
        #endregion
        #region 删除实体（定义为objectid的括展函数）
        public static void Remove(this ObjectId id)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Entity ent = tran.GetObject(id, OpenMode.ForWrite) as Entity;
                if (ent != null)
                {
                    ent.Erase();
                }
                tran.Commit();
            }
        }
        #endregion
        #region 矩形阵列实体（定义为objectid的括展函数）
        public static ObjectIdCollection ArrayRectang(this ObjectId id, int numRows, int numCols, double disRows, double disCols)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectIdCollection ids = new ObjectIdCollection();
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Entity ent = tran.GetObject(id, OpenMode.ForRead) as Entity;
                for (int m = 0; m < numRows; m++)
                {
                    for (int n = 0; n < numCols; n++)
                    {
                        Matrix3d mt = Matrix3d.Displacement(new Vector3d(n * disCols, m * disRows, 0));
                        Entity entCopy = ent.GetTransformedCopy(mt);
                        ObjectId entCopyId = id.Database.AddToModelSpace(entCopy);
                        ids.Add(entCopyId);
                    }
                }
                ent.UpgradeOpen();
                ent.Erase();
                tran.Commit();
            }
            return ids;
        }
        #endregion
        #region 镜像实体（定义为objectid的括展函数）
        public static ObjectId Mirror(this ObjectId id, Point3d mirrorPt1, Point3d mirrorPt2, bool eraseSourceObject)
        {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = doc.Database;
            ObjectId mirrorId = id;
            using (Transaction tran = db.TransactionManager.StartTransaction())
            {
                Line3d miLine = new Line3d(mirrorPt1, mirrorPt2);
                Matrix3d mt = Matrix3d.Mirroring(miLine);
                Entity ent = tran.GetObject(id, OpenMode.ForWrite) as Entity;
                if (eraseSourceObject == true)
                {
                    ent.TransformBy(mt);
                }
                else
                {
                    Entity entCopy = ent.GetTransformedCopy(mt);
                    mirrorId = db.AddToModelSpace(entCopy);
                }
                tran.Commit();
            }
            return mirrorId;
        }
        #endregion
    }
}
